package yourobot.robots;

import java.util.Random;

import org.jbox2d.callbacks.QueryCallback;
import org.jbox2d.collision.AABB;
import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.Fixture;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import yourobot.Level;
import yourobot.Main;

/**
 * 
 * @author mehdimasrour
 *
 */
public class IA {

	private final Body corps;
	public final static int DEFAULT_RADIUS = 25;
	
	private final World w;
	
	public IA(World w, Level l){
		this.w = w;
		BodyDef bd = new BodyDef();
		bd.type = BodyType.DYNAMIC;
		Random r = new Random(System.nanoTime());
		float x;
		float y;
		do{
			x = r.nextFloat()*Main.MAX_X-75;
			y = r.nextFloat()*Main.MAX_Y-75;
		} while( x<25 || y<25 || isTooClose(x, y, l) );
		bd.position.set(x, y);
		corps = w.createBody(bd);
		CircleShape c = new CircleShape();
		c.m_radius = DEFAULT_RADIUS;
		FixtureDef fd = new FixtureDef();
		fd.userData = "ia";
		fd.shape = c;
		fd.restitution = 0.1f;
		corps.createFixture(fd);
		corps.setLinearDamping(0.2f);
		corps.setLinearVelocity(new Vec2(125,125));
	}
	
	/**
	 * 
	 * @return the position of the IA
	 */
	public Vec2 getPosition(){
		return corps.getPosition();
	}
	
	public boolean isTooClose(float x, float y, Level l){
		float x_start = l.getStart().x;
		float y_start = l.getStart().y;
		return ( Math.abs(x_start-x)<150 && Math.abs(y_start-y)<150 );
	}
	
	/**
	 * Moves the IA
	 */
	public void move(){
		w.queryAABB(new QueryCallback() {
			
			@Override
			public boolean reportFixture(Fixture fixture) {
				if (fixture.getUserData().equals("player") ){
					Player p = (Player)fixture.getBody().getUserData();
					if ( !p.isDead() ){
						/*
						 * if the player we spotted is dead
						 * don't lose your time trying to kill him !
						 */
						float angle = (float) Math.atan2(p.getPosition().y - getPosition().y, p.getPosition().x - getPosition().x);
						corps.applyLinearImpulse(new Vec2((float)(10*Math.cos(angle)),(float)(10*Math.sin(angle))), getPosition());
						return true;
					}
				}
				Random r = new Random(System.nanoTime());
				int i = r.nextInt(100);
				if ( i == 5 ){
					switch ( r.nextInt(4) ){
					case 0:corps.setLinearVelocity(new Vec2(125,0));break;
					case 1:corps.setLinearVelocity(new Vec2(-125, 0));break;
					case 2:corps.setLinearVelocity(new Vec2(0, 125));break;
					case 3:corps.setLinearVelocity(new Vec2(0, -125));break;
					}
				}
				return true;
			}
		}, new AABB(new Vec2(getPosition().x - (float)(Math.sqrt(Math.pow(Main.MAX_X, 2)+Math.pow(Main.MAX_Y, 2))/4), 
				getPosition().y - (float)(Math.sqrt(Math.pow(Main.MAX_X, 2)+Math.pow(Main.MAX_Y, 2))/4)), 
				
				new Vec2(getPosition().x + (float)(Math.sqrt(Math.pow(Main.MAX_X, 2)+Math.pow(Main.MAX_Y, 2))/4), 
						getPosition().y + (float)(Math.sqrt(Math.pow(Main.MAX_X, 2)+Math.pow(Main.MAX_Y, 2))/4))));
	}
}
